/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.power.service.impl;

import me.zhengjie.modules.power.domain.EmployeeUser;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.power.repository.EmployeeUserRepository;
import me.zhengjie.modules.power.service.EmployeeUserService;
import me.zhengjie.modules.power.service.dto.EmployeeUserDto;
import me.zhengjie.modules.power.service.dto.EmployeeUserQueryCriteria;
import me.zhengjie.modules.power.service.mapstruct.EmployeeUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author RuitaoWei
* @date 2021-11-15
**/
@Service
@RequiredArgsConstructor
public class EmployeeUserServiceImpl implements EmployeeUserService {

    private final EmployeeUserRepository employeeUserRepository;
    private final EmployeeUserMapper employeeUserMapper;

    @Override
    public Map<String,Object> queryAll(EmployeeUserQueryCriteria criteria, Pageable pageable){
        Page<EmployeeUser> page = employeeUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeeUserMapper::toDto));
    }

    @Override
    public List<EmployeeUserDto> queryAll(EmployeeUserQueryCriteria criteria){
        return employeeUserMapper.toDto(employeeUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public EmployeeUserDto findById(String username) {
        EmployeeUser employeeUser = employeeUserRepository.findById(username).orElseGet(EmployeeUser::new);
        ValidationUtil.isNull(employeeUser.getUsername(),"EmployeeUser","username",username);
        return employeeUserMapper.toDto(employeeUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EmployeeUserDto create(EmployeeUser resources) {
        return employeeUserMapper.toDto(employeeUserRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeeUser resources) {
        EmployeeUser employeeUser = employeeUserRepository.findById(resources.getUsername()).orElseGet(EmployeeUser::new);
        ValidationUtil.isNull( employeeUser.getUsername(),"EmployeeUser","id",resources.getUsername());
        employeeUser.copy(resources);
        employeeUserRepository.save(employeeUser);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String username : ids) {
            employeeUserRepository.deleteById(username);
        }
    }

    @Override
    public void download(List<EmployeeUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeeUserDto employeeUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("密码", employeeUser.getPassword());
            map.put("手机号", employeeUser.getPhone());
            map.put("名字", employeeUser.getNickname());
            map.put("邮箱", employeeUser.getEmailaddress());
            map.put("性别", employeeUser.getGender());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}